﻿#include "client/clientwindow.h"
#include "ui_clientwindow.h"
#include "client/clientthread.h"
#include "utils.h"
#include <cstring>
#include <QMessageBox>
#include <QDebug>

ClientWindow::ClientWindow(QWidget * parent) : QWidget(parent){
    InitWindow();
    InitCustomSignalConn();
}

void ClientWindow::InitWindow(){
    InitUiConn();
    InitSubThread();
}

void ClientWindow::InitUiConn(){
    this->ui = new Ui::ClientWindow;
    ui->setupUi(this);
    connect(ui->pushButtonConnect, &QPushButton::clicked, this, &ClientWindow::OnPushButtonConnectClicked);
    connect(ui->pushButtonSend, &QPushButton::clicked, this, &ClientWindow::OnPushButtonSendClicked);
    connect(ui->pushButtonShowPrikey, &QPushButton::clicked, this, &ClientWindow::OnPushButtonShowPrikeyClicked);
    connect(ui->pushButtonShowPubkey, &QPushButton::clicked, this, &ClientWindow::OnPushButtonShowPubkeyClicked);
}

void ClientWindow::InitSubThread(){
    std::string pubkey, prikey;
    utils_rsa::GenerateRsaKey(prikey, pubkey, 2048);

    qDebug()<<"Pubkey: \n" << pubkey.c_str() << "\nPrikey:\n" << prikey.c_str();

    this->pubkey_ = pubkey;
    this->prikey_ = prikey;
    this->client_thread_ = new ClientThread(this, pubkey, prikey, this);
    this->client_thread_->start();
}

void ClientWindow::OnPushButtonSendClicked(void){
    QString msg = ui->textEditSendInput->toPlainText();
    emit this->client_thread_->SignalSendMessage(msg.toUtf8().constData());
}

void ClientWindow::OnPushButtonConnectClicked(void){
    if(client_thread_->getIsConnected()){
        QMessageBox::StandardButton btn = QMessageBox::question(this, "警告", "已经连接，是否断开连接并重新配置？");
        if(btn == false){
            QMessageBox::information(this, "提示", "操作已取消");
            return;
        }
    }
    unsigned int local_port, server_port, local_id, target_id;
    QString server_ip;

    local_port = ui->lineEditLocalPort->text().toUInt();
    server_port = ui->lineEditServerPort->text().toUInt();
    local_id = ui->lineEditLocalId->text().toUInt();
    target_id = ui->lineEditTargetId->text().toUInt();
    server_ip = ui->lineEditServerIp->text();

    if(local_port > 65535){
        local_port = 65535;
        char* buf = new char[16];
        sprintf(buf, "%d", local_port);
        ui->lineEditLocalPort->setText(buf);
        delete[] buf;
        QMessageBox::information(this, "警告", "本机端口号不大于65535，已强行设置为65535！");
    }

    if(server_port > 65535){
        QMessageBox::information(this, "警告", "服务器端口号不大于65535！");
        return;
    }

    if(!utils_sock::CheckIpAddrFormat(server_ip.toStdString())){
        QMessageBox::information(this, "警告", "IP地址不符合格式！\nIP地址应当为a.b.c.d，其中a、b、c均为范围为0-255的数字");
        return;
    }

    QMessageBox::StandardButton btn = QMessageBox::question(this, "确认信息", "是否确认？");
    if(btn == QMessageBox::Yes){
        QMessageBox::information(this, "正在连接", "请按下确定后耐心等待");
        emit client_thread_->SignalReconfigureNetwork(local_port, server_ip.toUtf8().data(), server_port, local_id, target_id);
    } else {
        QMessageBox::information(this, "提示", "操作已取消");
    }
}

void ClientWindow::OnReceiveMessage(const char* msg){
    ui->textBrowserOutput->append(msg);
}

void ClientWindow::OnPushButtonShowPubkeyClicked(){
    QMessageBox::information(this, "公玥", pubkey_.c_str());
}

void ClientWindow::OnPushButtonShowPrikeyClicked(){
    QMessageBox::information(this, "私玥", prikey_.c_str());
}

void ClientWindow::InitCustomSignalConn(){
    connect(this, &ClientWindow::SignalShowSysMessage, this, &ClientWindow::OnShowSysMessage);
    connect(this, &ClientWindow::SignalReceiveMessage, this, &ClientWindow::OnReceiveMessage);
    connect(this, &ClientWindow::SignalPrevSendComplete, this, &ClientWindow::OnPrevSendComplete);
}

void ClientWindow::OnShowSysMessage(const char* msg){
    ui->textBrowserOutput->append(msg);
}

void ClientWindow::OnPrevSendComplete(void){
    ui->textBrowserOutput->append("发送完成！\n");
}
